package com.study.mythread.model;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @program: java-study-of-learner
 * @description: 线程池callable和FutureTask
 * @author: future
 * @create: 2021-01-16 19:34
 **/
public class MyPoolThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建线程池
        ExecutorService pool1 = Executors.newFixedThreadPool(5);
        //2.创建任务
        List<Future> list1 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Callable c2 = new callable2();
            Future f1 = pool1.submit(c2);
            list1.add(f1);
        }
        // 关闭线程池
        pool1.shutdown();

        // 获取所有并发任务的运行结果
        for (Future f : list1) {
            // 从Future对象上获取任务的返回值，并输出到控制台
            System.out.println(">>>" + f.get());
        }


    }

}
class callable2 implements Callable<Integer>{


    public Integer call() throws Exception {
        int num =1;
        while(num!=20){
            System.out.println(Thread.currentThread().getName()+" callable2输出 "+ num++);
        }
        return Integer.valueOf(num);
    }

}
